/**
 * Copyright (c) 2012, lincomb Technology CO,LDT. All rights reserved.
 * Module: App推送
 * File: PushMessageServiceImpl.java
 * Purpose: 处理App推送消息Service实现层
 * Date: 2016-6-6
 */
package com.edais.service.imp;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.edais.dao.PushMessageDao;
import com.edais.domain.AfficheTab;
import com.edais.domain.PushMessageRecord;
import com.edais.domain.UnReadMsgInfo;
import com.edais.service.PushMessageService;
import com.edais.util.CommonConstant;
import com.edais.util.StringUtil;
import com.yunzhi.domain.RequestHeader;
import com.yunzhi.domain.ResultCode;
import com.yunzhi.domain.ResultDTO;
/**
 * ClassName: PushMessageServiceImpl 
 * @Description: App推送消息Service实现类
 * @author ZuoJun
 * @date 2016-6-6 17:23
 */
@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class PushMessageServiceImpl implements PushMessageService {
	
	private static final Logger logger = Logger.getLogger(PushMessageServiceImpl.class);

	@Autowired
	private PushMessageDao pushMessageDao;
	
	/**
	 * @Description: 处理保存手机设备信息业务
	 * @param param
	 * @author ZuoJun
	 * @date 2016-6-6 16:10
	 */
	@Override
	public ResultDTO saveMobileInfo(JSONObject param,RequestHeader header) {

		//根据CID查询手机设备信息
		Map<String,Object> queryParam = new HashMap<>();
		queryParam.put("cid",param.getString("cid"));
		List<Map<String, Object>> deviceInfo = pushMessageDao.queryDeviceInfoByDeviceNum(queryParam);

		String userId = header.userId2String();
		String status = "1";

		//若参数userId为空,设置默认值为0
		if(StringUtil.isEmpty(userId)){
			userId = "0";
			param.put("userId", userId);
			status = "0";
		}

		param.put("status", status);
		
		if(deviceInfo == null || deviceInfo.size() <= 0){
			Map<String,Object> saveParam = new HashMap<>();
			saveParam.put("cid",param.getString("cid"));
			saveParam.put("userId",param.getString("userId"));
			saveParam.put("status",param.getString("status"));
			pushMessageDao.saveMobileInfo(saveParam);
			logger.info("------------------------------------------GeTuiServiceImpl saveMobileInfo() 保存手机设备信息成功!-----------------------------------------------------");
			return new ResultDTO(ResultCode.SUCCESS,"手机设备信息保存成功!");
		}
		
		Map<String, Object> info = deviceInfo.get(0);
					
		String qUserId = String.valueOf(info.get("USERID"));
		
		//参数userId与查询出userId不一致
		if(!userId.equals(qUserId)){
			//更新手机设备信息
			Map<String,Object> updateParam = new HashMap<>();
			updateParam.put("cid",param.getString("cid"));
			updateParam.put("userId",param.getString("userId"));
			updateParam.put("status",param.getString("status"));
			pushMessageDao.updateMobileInfo(updateParam);
			logger.info("-------------------------------- GeTuiServiceImpl saveMobileInfo() userId=" + userId
					+ " , qUserId=" + qUserId + " 更新手机设备信息成功! --------------------------------");
		}
		return new ResultDTO(ResultCode.SUCCESS,"手机设备信息保存成功!");
	}

	public void unbindOtherMobile(JSONObject params, RequestHeader header){
		String logInfo = "手机设备信息-解绑其他手机-"+params.getString("cid") + "-" + header.getUserId() + "-";
		logger.info(logInfo + "start");
		if(null == header.getUserId() || 0 == header.getUserId()){
			return;
		}
		if(null == params.get("cid") || StringUtil.isEmpty(params.getString("cid"))){
			return;
		}
		Map<String,Object> delParam = new HashMap<>();
		delParam.put("cid",params.getString("cid"));
		delParam.put("userId",header.getUserId());
		this.pushMessageDao.unbindOtherMobile(delParam);
		logger.info(logInfo + "end");
	}
	/**
	 * @Description: 处理解除设备绑定业务
	 * @param params
	 * @param header
	 * @author ZuoJun
	 * @date 2016-7-15 15:40
	 */
	@Override
	public ResultDTO unbindMobile(JSONObject params, RequestHeader header) {

		Map<String,Object> updateParams = new HashMap<>();
		updateParams.put("status", 0);
		
		/**
		 * 解除设备绑定业务说明:
		 * 1.将userId置为0
		 * 2.新增查询条件unbindUserId
		 */
		updateParams.put("userId", "0");
		updateParams.put("unbindUserId", header.getUserId());
		pushMessageDao.updateMobileInfo(updateParams);
		logger.info("------------------------------------------------GeTuiServiceImpl unbindMobile() unbindUserId=" + header.getUserId() + " 解除设备绑定成功!-------------------------------------------------");
		return new ResultDTO(ResultCode.SUCCESS,"解除设备绑定成功!");
	}

	

	/**
	 * @Description: 处理根据用户ID、消息类别查询消息或公告信息业务
	 * @param params
	 * @return ResultDTO<Map<String, Object>>
	 * @author gongyanlan
	 * @date 2017-2-16
	 * 	 */
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	@Override
	public ResultDTO<PushMessageRecord> queryMessage(Map<String, Object> params){
		ResultDTO<PushMessageRecord> resultDTO = null;
		int totalNum = pushMessageDao.queryMessageCountByType(params);
		logger.info("/push/queryMessage PushMessageServiceImpl queryMessage() totalNum=" + totalNum);
		List<PushMessageRecord> message = pushMessageDao.queryMessageByType(params);
		int	 pageSize=0;
		//页面大小
		if(StringUtil.isEmpty(String.valueOf(params.get("pageSize")))){
			 pageSize = 10;//设置默认值为10
		}else{
			 pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
		}
		resultDTO = new ResultDTO<PushMessageRecord>(ResultCode.SUCCESS, pageSize,totalNum, message);
		return resultDTO;
	}

	/**
	 * @Description: 处理查询媒体报道信息业务
	 * @param params
	 * @return ResultDTO<AfficheTab> 
	 * @author gongyanlan
	 * @date 2017-2-16
	 */
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	@Override
	public ResultDTO<AfficheTab> queryMediaReport(Map<String, Object> params){
		int	 pageSize=0;
		//页面大小
		if(StringUtil.isEmpty(String.valueOf(params.get("pageSize")))){
			 pageSize = 10;//设置默认值为10
		}else{
			 pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
		}
		 ResultDTO<AfficheTab>  resultDTO = null;
		int totalNum = pushMessageDao.queryMediaReportCountByType(params);
		logger.info("/push/queryMessage PushMessageServiceImpl queryMediaReport() totalNum=" + totalNum );
		List<AfficheTab> mediaReport = pushMessageDao.queryMediaReportByType(params);
		resultDTO = new ResultDTO<AfficheTab>(ResultCode.SUCCESS, pageSize ,totalNum, mediaReport);
		return resultDTO;
	}

	/**
	 * @Description: 根据用户ID查询未读信息数
	 * @param params
	 * @return List
	 * @author gongyanlan
	 * @date 2017-2-16
	 */
	@Override
	public  List<UnReadMsgInfo> queryUnReadNumList(Map<String, Object> params){
		return pushMessageDao.queryUnReadNumList(params);
	}
	
	/**
	 * @Description: 清除用户未读信息数
	 * @param params
	 * @author WuHan
	 * @date 2016-6-7 21:18:00
	 */
	@Override
	public void clearUnReadNum(Map<String, Object> params) {
		// TODO Auto-generated method stub
		pushMessageDao.clearUnReadNum(params);
	}
	
	/**
	 * 查询媒体报道详细信息
	 * @param params
	 * @return
	 */
	@Override
	public Map<String, Object> queryMediaReportDetail(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return pushMessageDao.queryMediaReportDetail(params);
	}

}
